<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.sycit.cn
// +----------------------------------------------------------------------
// | Author: Peter.Zhang  <hyzwd@outlook.com>
// +----------------------------------------------------------------------
// | Date:   2017/8/25
// +----------------------------------------------------------------------
// | Title:  Customers.php
// +----------------------------------------------------------------------
namespace app\index\controller;

use think\Db;
use think\Loader;
use think\Request;
use think\Session;
use app\index\model\Customers AS CustomersModel;
use app\index\model\CustomersPremises;
use app\index\model\Purchase;
use app\index\model\Logistics;
use app\index\model\Finance AS FinanceModel;
use think\Url;

class Customers extends Common_base
{
    public function _initialize()
    {
        // 是否有权限
        IS_ROOT([1,2])  ? true : $this->error('没有权限');
        return parent::_initialize(); // TODO: Change the autogenerated stub

    }

    public function index()
    {
        $Customers = new CustomersModel();
        $request = Request::instance();
        $query = $request->param(); // 分页查询传参数
        $status = $request->param('status'); // 状态查询
        $q = $request->param('q'); // 企业名称查询

        if ($q !== '') {
            $data = $Customers->where('cus_name|cus_duty', 'like', '%'.$q.'%')->where('status','>=',0)->paginate('', false, ['query' => $query ]); // 企业名称查询
        } else {
            $data = $Customers->where('status','>=',0)->paginate(20); // 默认查询
        }

        // 获取分页显示
        $page = $data->render();
        $this->assign('page',$page);
        $this->assign('data', $data);
        $this->assign('empty', '<tr><td colspan="9" align="center">当前条件没有查到数据</td></tr>');
        $this->assign('title', '客户信息');
        return $this->fetch();
    }

    //添加
    public function add()
    {
        // 职员
        //$user = Db::name('users')->field('id,user_nick')->order('id','desc')->select();
        // 企业类型
        $property=Db::name('customers_type')->order('ty_id', 'asc')->select();
        // 评估等级
        $evaluate=Db::name('customers_evaluate')->order('eva_id', 'asc')->select();
        $this->assign('title', '新增客户');
        //$this->assign('user', $user);
        $this->assign('property', $property);
        $this->assign('evaluate', $evaluate);
        return $this->fetch();
    }

    //添加提交
    public function add_do()
    {
        // 设定数据返回格式
        //\think\Config::set("default_return_type","json");
        $request=Request::instance();
        if ($request->isPost()) {
            //$name = cutstr_html($request->param('name'));
            $duty = cutstr_html($request->param('duty'));

            $Customers = new CustomersModel();

            $validate = Loader::validate('Customers');
            if (!$validate->scene("add")->check($request->param())) {
                //$this->error($validate->getError());
            }
            $city = $request->param('city');
            $dist = $request->param('dist');
            $cus_city = isset($city) ? $city : '';
            $cus_dist = isset($dist) ? $dist : '';

            $data = [
                //'cus_have_uid' => $request->param('haveuid'),
                'cus_name' => $request->param('name'),
                'cus_duty' => $duty,
                //'cus_property' => $request->param('property'),
                //'cus_ty_id' => $request->param('type'),
                //'cus_eva_id' => $request->param('evaluate'),
                //'cus_img' => $request->param('fileimg'),
                'cus_phome' => $request->param('phome'),
                'cus_fax' => $request->param('fax'),
                'cus_moble' => $request->param('moble'),
                'cus_email' => $request->param('email'),
                'cus_http' => $request->param('http'),
                'cus_code' => $request->param('code'),
                'cus_prov' => $request->param('prov'),
                'cus_city' => $cus_city,
                'cus_dist' => $cus_dist,
                'cus_log_id'=> $request->param('cus_log_id'), //物流ID
                'cus_street' => $request->param('street'),
                'status' => 1
            ];
            //$addsert = $Customers->data($data);
            $result = $Customers->data($data)->save();
            if ($result) {
                //$Customers->cus_id;
                Db::name('customers_message')->insert(['msg_cus_id'=>$Customers->cus_id, 'msg_content'=>cutstr_html($request->param('content'))]);
                $this->success($duty.' 添加成功',Url::build('customers/index'));
            } else {
                $this->error('数据操作有错误');
            }
        }
    }

    //修改企业信息
    public function edit() {
        $Request = Request::instance();
        $id = $Request->param('id');
        if (!is_numeric($id) || empty($id)) {
            $this->error('参数错误！');
        }
        $data = Db::name('customers')->where('cus_id', $id)->find();
        if (empty($data)) {
            $this->error('参数错误！');
        }
        // 企业类型
        //$property=Db::name('customers_type')->order('ty_id', 'asc')->select();
        // 评估等级
        //$evaluate=Db::name('customers_evaluate')->order('eva_id', 'asc')->select();
        //物流信息
        $Logistics = new Logistics();
        $dataLog = $Logistics::get($data['cus_log_id']);
        //备注信息
        $message = Db::name('customers_message')->where('msg_cus_id', $id)->find();
        $assign = array(
            'title' => '修改客户信息',
            //'property' => $property,
            //'evaluate' => $evaluate,
            'data' => $data,
            'dataLog' => $dataLog,
            'msg' => $message,
        );
        $this->assign($assign);
        return $this->fetch();
    }

    //提交修改企业信息
    public function edit_do() {
        $Request = Request::instance();
        $id = $Request->param('cus_id');
        if (!is_numeric($id) || empty($id)) {
            $this->error('参数错误！');
        }
        $By = Db::name('customers')->where('cus_id', $id)->find();
        if (empty($By)) {
            $this->error('参数错误！');
        }
        $dist = $Request->param('dist');
        $dist = isset($dist) ? $dist : '';
        $data = [
            'cus_name' => $Request->param('name'),
            'cus_duty' => $Request->param('duty'),
            //'cus_property' => $Request->param('property'),
            //'cus_ty_id' => $Request->param('type'),
            //'cus_eva_id' => $Request->param('evaluate'),
            //'cus_img' => $Request->param(''),
            'cus_phome' => $Request->param('phome'),
            'cus_fax' => $Request->param('fax'),
            'cus_moble' => $Request->param('moble'),
            'cus_email' => $Request->param('email'),
            'cus_http' => $Request->param('http'),
            'cus_code' => $Request->param('code'),
            'cus_prov' => $Request->param('prov'),
            'cus_city' => $Request->param('city'),
            'cus_dist' => $dist,
            'cus_log_id'=> $Request->param('cus_log_id'), //物流ID
            'cus_street' => $Request->param('street'),
            //'status' => ,
        ];
        Db::name('customers')->where('cus_id', $id)->update($data);
        Db::name('customers_message')->where('msg_cus_id', $id)->update(['msg_content'=>cutstr_html($Request->param('content'))]);
        $this->success('修改成功', Url::build('customers/index'));
    }

    //查看基本信息
    public function view() {
        $Request = Request::instance();
        $id = $Request->param('id');
        if (!is_numeric($id) || empty($id)) {
            $this->error('参数错误！');
        }
        $Customers = new CustomersModel();
        $data = $Customers->where('cus_id', $id)->find();
        //备注信息
        $message = Db::name('customers_message')->where('msg_cus_id', $id)->find();
        //联系人
        $contact = Db::name('customers_contact')->where('con_cus_id', $id)->where('status','>=','1')->select();
        //默认收货信息
        //$premises = CustomersPremises::where('pre_cus_id', $id)->find();
        //历史订单
        $purchase = new Purchase();
        $lsdd = $purchase->where('pcus_id', $id)->order('pstart_date','deas')->paginate();

        //遍历获取订金金额
        foreach ($lsdd AS $key=>$val) {
            //订单订金
            $amo_dj = FinanceModel::where('fpnumber',$val['pnumber'])->where('sort',1)->sum('amount');
            $lsdd[$key]['amo_dj'] = '￥'.number_format($amo_dj,2);
            //订单余款
            $amo_yk = FinanceModel::where('fpnumber',$val['pnumber'])->where('sort',2)->sum('amount');
            $lsdd[$key]['amo_yk'] = '￥'.number_format($amo_yk,2);
        }

        // 历史订单分页
        $page_l = $lsdd->render();

        $assign = array(
            'title' => '查看信息',
            'data' => $data,
            'msg' => $message,
            'contact' => $contact,
            //'premises' => $premises,
            'lsdd' => $lsdd,
            'page_l' => $page_l,
            'empty_con' => '<tr><td colspan="8" align="center">当前条件没有查到数据</td></tr>',
            'empty_lsdd' => '<tr><td colspan="9" align="center">当前客户还没有订单</td></tr>',
        );
        $this->assign($assign);
        return $this->fetch();
    }

    //修改默认联系人
    public function adduser() {
        $Request = Request::instance();
        if ($Request->isPost()) {
            $conid = $Request->param('con');
            $cusid = $Request->param('cus');
            if (!is_numeric($conid) || empty($conid) && is_numeric($cusid) || empty($cusid)) {
                $this->error('参数错误！');
            }
            $result = Db::name('customers')->where('cus_id', $cusid)->update(['cus_con_id'=>$conid]);
            if ($result) {
                $this->success('设定默认联系人成功');
            } else {
                $this->error('设定错误！');
            }
        }
    }

    //删除操作
    public function delete() {
        // 是否有权限
        IS_ROOT([1])  ? true : $this->error('没有权限');
        $Request = Request::instance();
        if ($Request->isPost()) {
            $uid = $Request->param("uid");
            $name = $Request->param("name");
            if (empty($uid)) {
                $this->error('传入参数错误');
            }
            if ($name == 'delone') {
                // 单条删除操作
                Db::name('customers')->where('cus_id', $uid)->update(['status'=>'-1']);
                $this->success('删除成功', Url::build('customers/index'));
            } elseif ($name == 'delallattr') {
                // 多条删除操作
                $arrUid = explode(",",$uid);
                if (!empty($arrUid)) {
                    $i=0;
                    foreach ($arrUid as $key=>$val) {
                        Db::name('customers')->where('cus_id', $val)->update(['status'=>'-1']);
                        $i++;
                    }
                    $this->success($i.' 条记录删除成功', Url::build('customers/index'));
                }
            } else {
                // 不执行操作
                $this->error('传入参数错误');
            }
        }
    }

    //增加企业时判断名称是否存在
//    public function check_name() {
//        $Request = Request::instance();
//        if ($Request->isPost()) {
//            $name = $Request->param("name");
//            // 查询是否有此账户
//            $userDb = Db::name('customers')->where('cus_name', $name)->find();
//            if ($userDb) {
//                return false;
//            } else {
//                return true;
//            }
//        }
//    }

    //导出Excel
    public function excel() {
        $customers = Db::name('customers')->order('cus_prov asc')->select();
        foreach ($customers as $kc=>$vc) {
            //$dataResult
            //统计有没下过订单
            $xiadan = Db::name('purchase')->where('pcus_id', $vc['cus_id'])->count();
            //查找附加联系人
            $lianxi = Db::name('customers_contact')->where('con_cus_id', $vc['cus_id'])->find();

            $dataResult[$kc]['cus_prov'] = $vc['cus_prov'];
            $dataResult[$kc]['cus_city'] = $vc['cus_city'];
            $dataResult[$kc]['cus_dist'] = $vc['cus_dist'];
            $dataResult[$kc]['cus_street'] = $vc['cus_street'];
            $dataResult[$kc]['cus_name'] = $vc['cus_name'];
            //下单状况
            if ($xiadan > 0) {
                $dataResult[$kc]['status'] = '有下单';
            } else {
                $dataResult[$kc]['status'] = '未下单';
            }

            $dataResult[$kc]['cus_duty'] = $vc['cus_duty'];
            $dataResult[$kc]['cus_moble'] = $vc['cus_moble'];
            $dataResult[$kc]['cus_phome'] = $vc['cus_phome'];
            $dataResult[$kc]['cus_fax'] = $vc['cus_fax'];
            $dataResult[$kc]['qq'] = '';
            $dataResult[$kc]['cus_email'] = $vc['cus_email'];
            //第二联系人
            if (empty($lianxi)) {
                $dataResult[$kc]['lxr2'] = '';
                $dataResult[$kc]['lxdh2'] = '';
            } else {
                $dataResult[$kc]['lxr2'] = $lianxi['con_name'];
                $dataResult[$kc]['lxdh2'] = $lianxi['con_mobile'];
            }
            //物流信息
            $wuliu = Db::name('logistics')->where('log_id', $vc['cus_log_id'])->field('log_name,log_phone,log_address')->find();
            if (empty($wuliu)) {
                $dataResult[$kc]['log_name'] = '';
                $dataResult[$kc]['log_phone'] = '';
                $dataResult[$kc]['log_address'] = '';
            } else {
                $dataResult[$kc]['log_name'] = $wuliu['log_name'];
                $dataResult[$kc]['log_phone'] = $wuliu['log_phone'];
                $dataResult[$kc]['log_address'] = $wuliu['log_address'];
            }
        }
        $headTitle = \think\Config::get('syc_webname')." - 客户信息";
        $title = "客户信息-".date('Y-m-d');
        $headtitle= '<thead><tr style="height:50px;border-style:none;"><th border="0" style="height:60px;width:270px;font-size:22px;" colspan="17">'.$headTitle.'</th></tr>';
        $titlename = "<tr style='height:30px;'> 
               <th style='width:60px;'>省</th> 
               <th style='width:60px;'>市</th> 
               <th style='width:60px;'>区/县/镇</th> 
               <th style='width:150px;'>地址</th> 
               <th style='width:70px;'>单位</th> 
               <th style='width:50px;'>状态</th> 
               <th style='width:70px;'>联系人</th> 
               <th style='width:100px;'>手机</th> 
               <th style='width:70px;'>固话</th> 
               <th style='width:70px;'>传真</th> 
               <th style='width:90px;'>QQ</th> 
               <th style='width:90px;'>邮箱</th> 
               <th style='width:90px;'>联系人2</th> 
               <th style='width:90px;'>联系人2电话</th> 
               <th style='width:90px;'>物流名称</th> 
               <th style='width:90px;'>物流电话</th> 
               <th style='width:90px;'>物流地址</th>
           </tr></thead>";
        $filename = $title.".xls";
        $this->excelData($dataResult,$titlename,$headtitle,$filename);
    }

    /*
    *处理Excel导出
    *@param $datas array 设置表格数据
    *@param $titlename string 设置head
    *@param $title string 设置表头
    */
    public function excelData($datas,$titlename,$title,$filename){
        IS_ROOT([1,2,3,4])  ? true : $this->error('没有权限');
        $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
        $str .="<table border=1>".$title."";
        $str .= $titlename;
        $str .= "<tbody>";
        foreach ($datas  as $key=> $rt )
        {
            $str .= "<tr>";
            foreach ( $rt as $k => $v )
            {
                $str .= "<td>{$v}</td>";
            }
            $str .= "</tr>\n";
        }
        $str .= "</tbody>";
        $str .= "</table>\r\n</body>\r\n</html>";
        header( "Content-Type: application/vnd.ms-excel; name='excel'" );
        header( "Content-type: application/octet-stream" );
        header( "Content-Disposition: attachment; filename=".$filename );
        header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
        header( "Pragma: no-cache" );
        header( "Expires: 0" );
        exit( $str );
    }
}